#include"Include.h"

vu16 Tcr_buf;
vu8  KV,LowPow_EndTime;						
vu16 KI; 						
s16  BI;
void System_Init();
void IO_Init();
void Var_Init();

    

void  ClearMemory()
{
	_mp0=0x80;
	for(_tblp=0;_tblp<128;_tblp++)
	{
		_iar0=0;
		_mp0++;
	}
    _mp1h = 0x01;
    _mp1l = 0x80;
	for(_tblp=0;_tblp<128;_tblp++)
	{
		_iar1=0;
		_mp1l++;
	}
    _mp1h = 0x02;
    _mp1l = 0x80;
	for(_tblp = 0; _tblp < 128; _tblp++)
	{
		_iar1 = 0;
		_mp1l++;
	}
    _mp1h = 0x03;
    _mp1l = 0x80;
	for(_tblp = 0; _tblp < 128; _tblp++)
	{
		_iar1 = 0;
		_mp1l++;
	}		
}

void System_Init()
{
    _scc = 0x00; // Set the system clock to 16MHz
    _pscr=0b00000100;

    IO_Init();
    Timer0_Init();
    Timer1_Init();
    Timer2_Init();
    IGBT_Off();         // Turn off IGBT
    ADC_Init();         // ADC initialization
    CMP_OPA_Init();     // Amplifier and comparator initialization
    ISR_CMP_OPA_Init(); // Comparator interrupt initialization
    Beep_Init();        // Buzzer and fan initialization
    IIC_Init();
    PPG_Config();
    Var_Init(); // Variable initialization


}

void IO_Init()
{

	_pas16 = 0;  //select PA3 as CPOP
	_pas15 = 0;
	_pas14 = 1;
	
	_pas36 = 0;  //select PA7 as  AN5 (AD IGBT)
	_pas35 = 1; 
	_pas34 = 0;
	
	_pas32 = 0;  //select PA6 as  CP2N  (voltage surge)
	_pas31 = 0;
	_pas30 = 1;
	
	_pas22 = 0;  //select PA4 as  AN4 (AD PAN)
	_pas21 = 1;	    
	_pas20 = 1;
	_pac4 =1;
       
	_pas06 = 0;  //select PA1 as OPOUT & AN8
	_pas05 = 1;
	_pas04 = 1; 	
   
    _pas00 = 0; //select PA0 as SDA
	_pas01 = 1;	
	_pas00 = 0; 
     
	_pas12 = 0;  //select PA2 as SCL
	_pas11 = 0; 	  
	_pas10 = 1;
	
	_pas26 = 0;  //select PA5 as  PPG2
	_pas25 = 0;
	_pas24 = 1;

   	_pbs16 = 0;  //select PB3 as  CP0N
   	_pbs15 = 0;
   	_pbs14 = 1;
   
   	_pbs12 = 0;  //select PB2 CP4P&AN1 (ZCD)
  	_pbs11 = 1;	 
    _pbs10 = 1;
   
   	_pbs06 = 0;  //select PB1 as  PCK (BUZZER)
  	_pbs05 = 1;  
    _pbs04 = 0;
   
//    _pcs02 = 1; //select PC0 as PWM1(Fan)
//    _pcs01 = 1;
//    _pcs00 = 0;
    _pcs02 = 0; //select PC0 as IO
    _pcs01 = 0;
    _pcs00 = 0;
    _pcc0  = 0;
   
   	_pbs22 = 1; //select PB4 as  OPINN1
  	_pbs21 = 0;  
   	_pbs20 = 0; 
   	
   	_pcs12 = 1;	//select PC2 as CP5N (CURRENT SURGE)
   	_pcs11 = 1;
   	_pcs10 = 0;


	_pbs02 = 0; //select PB0 as PB0
	_pbs01 = 0;
	_pbs00 = 0;
	_pbc0 = 1;	// PB0 Input
	_pbpu0 = 1;	//PULL HIGH
	
#ifdef UART_ASCII
	_pcc1=0; //pc1 as debug pin
	
//	_pbc0=0;//pb0 as toggle pin
	
	_pcc3=0;//pc3 as io
	_pcs16=0;//pc3 as C3VOD
	_pcs15=1;
	_pcs14=0;
#else
//	_pbc0=0;//pb0 as toggle pin
//	_pcc1=0;
#endif

}

void Var_Init()
{
    PanMoveInDet = 1;
    AD_Vac = C_AD_215V;
    ////---- Power Control ----
    bPowerStepChanged = 1;
    PowerStep = C_PowStep_OFF;
    PPGTA_copy = C_InitPPGT; // C_MaxPPGT;	//smallest pulse
    PanDetPulseCnt = 250;
    R_PowerIV = 0;	
    Beep_Cmd=BEEP_1000MS;	
	LowPow_EndTime = C_LOWPOW_END_TIME;
	KV=121;						
	//KI=169; 	
	KI=166; 							
	BI=-1436;
}
